package com.jkoss.action.kc;

import java.util.Date;

import javax.servlet.http.HttpServletRequest;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import com.alibaba.fastjson.JSONObject;
import com.jkoss.biz.kc.couBiz;
import com.jkoss.pojo.kc.Course;
import com.jkoss.tool.Page;

@Controller
@RequestMapping("/kc")
public class CouAction {
    @Autowired
	private  couBiz cobiz;
	
    
     ///////////////////////课程设置表
     //显示全部
	@RequestMapping("listCou.do")
 	public String listCourse(HttpServletRequest req,Page<Course> page){
		if(page==null){
			page=new Page<Course>();
		}
		
 		page.setResults(cobiz.listCou(page));
		
 		req.setAttribute("page", page);
 		return "listCou.jsp";
 		
 	}
 	
 	//查看单个
 	@RequestMapping("oneCourse.do")
 	public String oneCourse(HttpServletRequest req,Integer kcid){
		Course cour=cobiz.findById(kcid);
		if(cour!=null){
		req.setAttribute("cour", cour);
		}
 		
 		return "shCou.jsp";
 		
 	}
 	
 	//删除单个
 	@RequestMapping("delCourse.do")
 	public String delCourse(HttpServletRequest req,Integer kcid){
		String msg=cobiz.delCou(kcid);
 		req.setAttribute("msg", msg);
 		return listCourse(req,null);
 	}
 	
 	//添加
 	@RequestMapping("addCourse.do")
 	public String addCourse(Course course,HttpServletRequest req ){
 		
 		course.setKcdate(new Date());
		String msg=cobiz.addCou(course);
 		req.setAttribute("msg", msg);
 		return listCourse(req,null);
 		
 	}
 	
 	//去修改显示的AJAX
 	@RequestMapping(value="/ajOneCourse.do",produces="text/html;charset=UTF-8")
 	@ResponseBody
 	public String toupCourse(Integer kcid){
 		
 		return   JSONObject.toJSONString(cobiz.findById(kcid)).toString();
 	}
 	
 	//修改
 	@RequestMapping("upCourse.do")
 	public String upCourse(Course course,HttpServletRequest req){
		Course c=cobiz.findById(course.getKcid());
 		c.setKcdate(new Date());
 		c.setKcname(course.getKcname());
 		c.setKcdesc(course.getKcdesc());
 		String msg=cobiz.upCou(c);
 		req.setAttribute("msg", msg);
 		return listCourse(req,null);
 		
 	}
}
